using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JTLeigh.Tools.Development.CopySourceAsHtml.UnitTests
{

    /// <summary>
    /// Unit tests for the <see cref="HtmlTools"/> class.
    /// </summary>
    [TestClass]
    public class HtmlToolsFixture
    {
        #region Methods

        #region HtmlEncode

        /// <summary>
        /// Ensures <see cref="HtmlTools.HtmlEncode(string)"/> correctly throws <see cref="ArgumentNullException"/>
        /// when called with a <see langword="null"/> value.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestHtmlEncodeWithNullValue()
        {
            HtmlTools.HtmlEncode(null);
        }

        /// <summary>
        /// Ensures that <see cref="HtmlTools.HtmlEncode(string)"/> encodes the specified 
        /// <see cref="String"/> value correctly.
        /// </summary>
        [TestMethod]
        public void TestHtmlEncode()
        {
            Assert.AreEqual("&quot;&amp;&lt;&gt; ~&#1024;?", HtmlTools.HtmlEncode("\"&<> ~\u0400\t"));
        }

        /// <summary>
        /// Ensures that <see cref="HtmlTools.HtmlEncode(char)"/> encodes the most common entity 
        /// characters as entities.
        /// </summary>
        [TestMethod]
        public void TestHtmlEncodeWithEntityCharacters()
        {
            Assert.AreEqual("&quot;", HtmlTools.HtmlEncode('"'));
            Assert.AreEqual("&amp;", HtmlTools.HtmlEncode('&'));
            Assert.AreEqual("&lt;", HtmlTools.HtmlEncode('<'));
            Assert.AreEqual("&gt;", HtmlTools.HtmlEncode('>'));
        }

        /// <summary>
        /// Ensures that <see cref="HtmlTools.HtmlEncode(char)"/> does not encode Latin characters.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Any character between ' ' and '~' should not be encoded.
        /// </para>
        /// </remarks>
        [TestMethod]
        public void TestHtmlEncodeWithLatinCharacters()
        {
            Assert.AreEqual(" ", HtmlTools.HtmlEncode(' '));
            // ...
            Assert.AreEqual("0", HtmlTools.HtmlEncode('0'));
            Assert.AreEqual("9", HtmlTools.HtmlEncode('9'));
            // ...
            Assert.AreEqual("A", HtmlTools.HtmlEncode('A'));
            Assert.AreEqual("Z", HtmlTools.HtmlEncode('Z'));
            Assert.AreEqual("a", HtmlTools.HtmlEncode('a'));
            Assert.AreEqual("z", HtmlTools.HtmlEncode('z'));
            // ...
            Assert.AreEqual("~", HtmlTools.HtmlEncode('~'));
        }

        /// <summary>
        /// Ensures that <see cref="HtmlTools.HtmlEncode(char)"/> encodes non-Latin characters as 
        /// numeric character references.
        /// </summary>
        [TestMethod]
        public void TestHtmlEncodeWithNonLatinCharacters()
        {
            Assert.AreEqual("&#1024;", HtmlTools.HtmlEncode((char)0x0400));
        }

        /// <summary>
        /// Ensures that <see cref="HtmlTools.HtmlEncode(char)"/> encodes control characters as a 
        /// question mark.
        /// </summary>
        [TestMethod]
        public void TestHtmlEncodeWithControlCharacters()
        {
            Assert.AreEqual("?", HtmlTools.HtmlEncode('\t'));
        }

        #endregion // HtmlEncode

        #endregion // Methods
    }

}
